package com.mdlib.helper.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.ljlib.help.R;
import com.mdlib.helper.model.entity.ShareEntity;
import com.mdlib.helper.utils.ShareUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/8/11 14:45
 * 邮箱：hqy_xz@126.com
 */
public class ShareDialogFragment extends DialogFragment {

    private static final String SHARE_INFO = "share_info";
    @Bind(R.id.ll_share_qq)
    LinearLayout mLlShareQq;
    @Bind(R.id.ll_share_zone)
    LinearLayout mLlShareZone;
    @Bind(R.id.ll_share_wechat)
    LinearLayout mLlShareWechat;
    @Bind(R.id.ll_share_firend)
    LinearLayout mLlShareFirend;
    @Bind(R.id.ll_share_close)
    LinearLayout mLlShareClose;
    @Bind(R.id.ll_live_share)
    LinearLayout mLlLiveShare;


    private ShareEntity shareEntity;
    private ShareUtils shareUtils;


    public static ShareDialogFragment newInstance(ShareEntity shareInfo) {
        ShareDialogFragment fragment = new ShareDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(SHARE_INFO, shareInfo);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            shareEntity = (ShareEntity) bundle.getSerializable(SHARE_INFO);
        }

    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Dialog_Shares);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_share, null);
        builder.setView(view);
        ButterKnife.bind(this, view);
        shareUtils = new ShareUtils(getActivity(), shareEntity);
        return builder.create();
    }


    @Override
    public void onStart() {
        super.onStart();

        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @OnClick({R.id.ll_share_qq, R.id.ll_share_zone, R.id.ll_share_wechat, R.id.ll_share_firend, R.id.ll_share_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_share_qq:
                shareUtils.QQShare("1");
                dismiss();
                break;
            case R.id.ll_share_zone:
                shareUtils.QQShare("2");
                dismiss();
                break;
            case R.id.ll_share_wechat:
                shareUtils.WxShare("1");
                dismiss();
                break;
            case R.id.ll_share_firend:
                shareUtils.WxShare("2");
                dismiss();
                break;
            case R.id.ll_share_close:
                dismiss();
                break;
        }
    }
}
